import 'package:flutter/cupertino.dart';
import 'package:intl/intl.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:kq_flutter_widgets/special/seekBarForScienceTestMold/seek_bar_for_science_test_mold_result.dart';
import 'package:kq_flutter_widgets/widgets/divider/kq_divider.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:kq_flutter_core_widget/widgets/image/kq_image.dart';
import 'package:kq_flutter_core_widget/utils/ex/kq_ex.dart';

import '../../resources/kq_theme_colors.dart';

/// @author 高裕
/// 封装好的科学试模结果模块(布局兼容平板)
class SimpleScienceTestMoldResult<T extends SimpleScienceTestMoldResultEntity>
    extends StatefulWidget {
  const SimpleScienceTestMoldResult({
    super.key,
    required this.entity,
  });

  final T entity;

  @override
  State<StatefulWidget> createState() => SimpleScienceTestMoldResultState();
}

class SimpleScienceTestMoldResultState
    extends State<SimpleScienceTestMoldResult> {
  final numberFormat = NumberFormat("#.##");

  @override
  void initState() {
    super.initState();
    widget.entity.state = this;
  }

  @override
  void didUpdateWidget(covariant SimpleScienceTestMoldResult oldWidget) {
    super.didUpdateWidget(oldWidget);
    widget.entity.state = this;
  }

  /// 主动刷新方法
  update() {
    if (mounted) {
      setState(() {});
    }
  }

  /// 主动清空方法
  clear() {
    if (mounted) {
      widget.entity.value = null;
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    if (widget.entity.min == null ||
        widget.entity.max == null ||
        widget.entity.title.isNullOrEmpty) {
      return Container();
    }
    return Container(
      width: double.infinity,
      height: 49.r,
      color: KqThemeColors.bgWhite,
      child: Column(
        children: [
          Expanded(
              child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              SizedBox(width: 16.r),
              if (widget.entity.imagePath.isNotNullOrEmpty)
                KqImage.assets(
                  url: widget.entity.imagePath!,
                  width: 16.r,
                  height: 16.r,
                ),
              const Expanded(
                flex: 4,
                child: SizedBox(),
              ),
              Expanded(
                flex: 72,
                child: Text(
                  widget.entity.title!,
                  style: TextStyle(
                    color: KqThemeColors.text26,
                    fontSize: 12.sp,
                  ),
                ),
              ),
              const Expanded(
                flex: 4,
                child: SizedBox(),
              ),
              Expanded(
                flex: 78,
                child: Text(
                  widget.entity.value == null
                      ? ''
                      : numberFormat.format(widget.entity.value),
                  style: TextStyle(
                    color: KqThemeColors.text26,
                    fontSize: 18.sp,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
              const Expanded(
                flex: 16,
                child: SizedBox(),
              ),
              Expanded(
                flex: 192,
                child: SeekBarForScienceTestMoldResult(
                  max: widget.entity.max!,
                  min: widget.entity.min!,
                  slider: Container(
                    width: 6.r,
                    height: 6.r,
                    decoration: BoxDecoration(
                      color: KqThemeColors.textWhite,
                      borderRadius: BorderRadius.all(Radius.circular(3.r)),
                    ),
                  ),
                  sliderWidth: 6.r,
                  sliderHeight: 6.r,
                  progressHeight: 6.r,
                  textOffSet: 2.r,
                  value: widget.entity.value,
                  canEdit: false,
                ),
              ),
              SizedBox(width: 16.r),
            ],
          )),
          KqDivider(height: 1.r),
        ],
      ),
    );
  }
}

/// 科学试模结果模块 抽象类
abstract class SimpleScienceTestMoldResultEntity {
  /// 当前值
  double? get value;

  set value(double? value);

  /// 最大值(可溢出)
  double? get max;

  /// 最小值(可溢出)
  double? get min;

  /// 标题
  String? get title;

  /// 图片路径
  String? get imagePath;

  /// 一般用于刷新和清空
  @JsonKey(includeFromJson: false, includeToJson: false)
  SimpleScienceTestMoldResultState? state;
}

/// 科学试模结果模块(在没实体可以继承时使用的预设)
class NormalSimpleScienceTestMoldResultEntity
    extends SimpleScienceTestMoldResultEntity {
  NormalSimpleScienceTestMoldResultEntity(
      {required this.imagePath,
      required this.max,
      required this.min,
      required this.title,
      this.value});

  @override
  String? imagePath;

  @override
  double? max;

  @override
  double? min;

  @override
  String? title;

  @override
  double? value;
}
